package learn.stack;

import java.util.Scanner;

/**
 * 用数组来实现栈
 * @author baoshi
 */
public class ArrayStackDemo {
    public static void main(String[] args) {
//        testStack();

    }

    private static void testStack() {
        ArrayStack stack = new ArrayStack(4);
        String key = "";
        //是否退出菜单
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);

        while (loop) {
            System.out.println("s(show)显示栈");
            System.out.println("e(exit)退出程序");
            System.out.println("p(push)添加数据到栈");
            System.out.println("pp(pop)取出栈数据");

            System.out.println("请输入你的选择");

            key = scanner.next();

            switch (key) {
                case "s":
                    try {
                        stack.list();
                    } catch (RuntimeException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "e":
                    scanner.close();
                    loop = false;
                    break;
                case "p":
                    System.out.println("输入添加的数字");
                    int number = scanner.nextInt();
                    try {
                        stack.push(number);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "pp":
                    try {
                        System.out.printf("出栈的数据stack[%d]=%d", stack.getTop(), stack.pop());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

/**
 * 表示栈结构的类
 */
class ArrayStack {
    /**
     * 栈的大小
     */
    private int maxSize;
    /**
     * 数组模拟栈，数据保存在数组中
     */
    private int[] stack;

    /**
     * 栈顶
     */
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int[] getStack() {
        return stack;
    }

    public void setStack(int[] stack) {
        this.stack = stack;
    }

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    /**
     * 判断栈是否已满
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 判断栈空
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 入栈
     *
     * @param num 入栈的参数
     * @throws Exception 栈满
     */
    public void push(int num) throws Exception {
        if (isFull()) {
            throw new RuntimeException("栈满");
        }
        top++;
        stack[top] = num;
    }

    /**
     * @return 出栈
     * @throws Exception 栈空
     */
    public int pop() throws Exception {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int num = stack[top];
        top--;
        return num;
    }

    /**
     * 遍历栈
     */
    public void list() throws RuntimeException {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }

        for (int i = top; i >-1; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }
}
